package com.zhudi.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhudi.annotation.UserLoginCheck;
import com.zhudi.commen.BaseResponse;
import com.zhudi.model.enums.ErrorCode;
import com.zhudi.expection.BusinessException;
import com.zhudi.model.entity.Team;
import com.zhudi.model.entity.User;
import com.zhudi.model.request.*;
import com.zhudi.model.vo.TeamVO;
import com.zhudi.service.TeamService;
import com.zhudi.service.UserService;
import com.zhudi.service.UserTeamService;
import com.zhudi.utils.ResultUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import java.util.List;

import static com.zhudi.contant.UserContant.USER_LOGIN_STATE;

@RestController
@RequestMapping("/team")
public class TeamController {

    @Resource
    private TeamService teamService;

    @Resource
    private UserService userService;

    @Resource
    private UserTeamService userTeamService;

    @PostMapping("/addTeam")
    @UserLoginCheck
    public BaseResponse<Long> addTeam(@RequestBody TeamCreateRequest teamCreateRequest,HttpServletRequest request){
        if(teamCreateRequest==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"参数为空");
        }
        User loginUser = (User) request.getSession().getAttribute(USER_LOGIN_STATE);
        if (loginUser==null){
            throw new BusinessException(ErrorCode.NOT_LOGIN);
        }
        //强制领队id和登录用户id一致
        Team newTeam = new Team();
        BeanUtils.copyProperties(teamCreateRequest,newTeam);
        newTeam.setLeaderId(loginUser.getId());
        newTeam.setPlayersNum(1);
        long result = teamService.addTeam(newTeam);
        return ResultUtils.result(result,result!=0);
    }

    @PostMapping("/updateTeam")
    @UserLoginCheck
    public BaseResponse<Boolean> updateTeam(@RequestBody TeamUpdateRequest teamUpdateRequest){
        if (teamUpdateRequest==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        //todo 增加只有队长才能修改队伍信息的校验
        return ResultUtils.success(teamService.updateTeam(teamUpdateRequest),"修改成功");
    }

    @PostMapping("/searchTeam")
    public BaseResponse<Page<TeamVO>> searchTeams(@RequestBody TeamSearchRequest teamSearchRequest, HttpServletRequest request){
        //1. 参数判空
        if(teamSearchRequest==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"参数为空");
        }

        //2. 页数检查，合法化
        int pageSize = teamSearchRequest.getPageSize();
        int pageNum = teamSearchRequest.getPageNum();
        if (pageNum<=0||pageSize<=0){
            pageNum =1;
            pageSize=10;
        }

        //3. 权限获取（在 service 层根据是否是管理员来过滤获取的信息）
        User loginUser = userService.getLoginUser(request);
        //4. 调用函数，返回结果
        Page<TeamVO> teamVO =teamService.searchTeamsByPage(teamSearchRequest, loginUser);



        return ResultUtils.success(teamVO,"搜索成功");
    }


    @PostMapping("/joinTeam")
    @UserLoginCheck
    public BaseResponse<Boolean> joinTeam(@RequestBody TeamJoinRequest teamJoinRequest ,HttpServletRequest request){
        //  1. 检验参数接受、队伍 id 是否为空
        if (teamJoinRequest==null||teamJoinRequest.getTeamId()==null||teamJoinRequest.getTeamId()<=0){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }

        //2. 获取登录用户id
        User loginUser = (User) request.getSession().getAttribute(USER_LOGIN_STATE);
        if (loginUser==null){
            throw new BusinessException(ErrorCode.NOT_LOGIN);
        }
        Long loginUserId = loginUser.getId();

        //3. 调用service

       Boolean save =  userTeamService.joinTeam(teamJoinRequest,loginUserId);
       return ResultUtils.success(save,"加入成功");

    }

    @PostMapping("/quitTeam")
    @UserLoginCheck
    public BaseResponse<Boolean> quitTeam(@RequestBody TeamQuitRequest teamQuitRequest,HttpServletRequest request){
        if (teamQuitRequest==null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = (User) request.getSession().getAttribute(USER_LOGIN_STATE);

        Long loginUserId = loginUser.getId();
        Long teamId = teamQuitRequest.getTeamId();

        return ResultUtils.result(userTeamService.quitTeam(teamId,loginUserId));
    }

    @PostMapping("/deleteTeam")
    @UserLoginCheck
    public BaseResponse<Boolean> deleteTeam(@RequestBody TeamQuitRequest teamQuitRequest,HttpServletRequest request){
        if (teamQuitRequest==null||teamQuitRequest.getTeamId()==null||teamQuitRequest.getTeamId()<=0){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);

        }
        User loginUser = (User) request.getSession().getAttribute(USER_LOGIN_STATE);

        Long loginUserId = loginUser.getId();
        Long teamId = teamQuitRequest.getTeamId();

        boolean result = userTeamService.deleteTeam(teamId,loginUserId);

        return ResultUtils.result(result);
    }


    @GetMapping("/getTeam")
    public BaseResponse<TeamVO> getTeam(long id,HttpServletRequest request){
        //可以加一个鉴权的功能，现在就先不加了
        if (id<=0){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }

        Team team = teamService.getById(id);
        if (team==null){
            //如果team等于空，说明要查找的队伍不存在。
            return ResultUtils.success(null,"该队伍不存在");
        }

        //team转为teamVO
        TeamVO teamVO = new TeamVO();
        BeanUtils.copyProperties(team,teamVO);
        return ResultUtils.success(teamVO,"成功获取队伍信息");
    }

    @GetMapping("/myJoinTeam")
    @UserLoginCheck
    public BaseResponse<List<TeamVO>> getMyJoinTeam(HttpServletRequest request){
        User loginUser = (User) request.getSession().getAttribute(USER_LOGIN_STATE);

        return ResultUtils.success(teamService.getMyJoinTeam(loginUser),"成功获取当前用户加入的队伍");

    }
}
